package com.admin.socketmultiplayer.activity

import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.SaveListener
import cn.bmob.v3.listener.UpdateListener
import com.admin.socketmultiplayer.R
import com.admin.socketmultiplayer.bean.MyUser
import com.admin.socketmultiplayer.dialog.PromptDialog
import com.admin.socketmultiplayer.inteface.DialogListener
import kotlinx.android.synthetic.main.activity_change_password.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/5/10.
 */
class ChangePasswordActivity:BaseActivity(){

    var prompt:PromptDialog?=null

    override fun bindLayout(): Int {
        return R.layout.activity_change_password
    }

    override fun initData() {
        iv_back.setImageResource(R.mipmap.return_normal)
        tv_title.text="密码修改"
    }

    override fun initEvent() {
        iv_back.setOnClickListener { finish() }

        btn_change_password.setOnClickListener {
            var old=edt_old_password.text.toString()
            if(old==""){
                toast("请输入旧密码")
            }else{
                var u=BmobUser.getCurrentUser()
                var myuser=MyUser()
                myuser.username=u.username
                myuser.setPassword(old)
                myuser.login(object:SaveListener<MyUser>(){
                    override fun done(p0: MyUser?, p1: BmobException?) {
                        p1?:let {
                            BmobUser.resetPasswordByEmail(u.email,object:UpdateListener(){
                                override fun done(p0: BmobException?) {
                                    p0?:let {
                                        showPrompt("已发送邮件到注册邮箱 ${u.email} 请登陆邮箱修改密码",successListener)
                                    }
                                }
                            })
                        }
                        p1?.let {
                            showPrompt("密码错误，请输入正确的密码",errorListener)
                        }
                    }
                })
            }
        }
    }

    fun showPrompt(content:String,listener:DialogListener){
        prompt= PromptDialog(getContext(),R.layout.dialog_prompt,listener,R.style.cusdialog)
        prompt!!.setPromptContent(content)
        prompt!!.show()
    }

    var errorListener=object :DialogListener{
        override fun enSureListener() {

        }

        override fun selectItemListener(any: Any?) {

        }

        override fun cancelListener() {

        }

    }


    var successListener=object :DialogListener{
        override fun enSureListener() {

        }

        override fun selectItemListener(any: Any?) {

        }

        override fun cancelListener() {
            MyApplication.finishAllActivity()
            startActivity<LoginActivity>()
            BmobUser.logOut()
            finish()
        }

    }
}